home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / GNU Diff Sources / GNU DIFF 1.15b Sources / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-08  |  7.4 KB  |  321 lines  |  [TEXT/ALFA]

  1. /*************************************************************************
  2.  *                                                                         *
  3.  *  Module:        unix main.c                                                 *
  4.  *    Programmer:    Steve Adams                                                 *
  5.  *                                                                         *
  6.  *  (C) Copyright 1985, THINK Technologies, Inc.  All rights reserved.   *
  7.  *                                                                         *
  8.  *  Alternate main progrUNIX Diff.πe Unix command lines under Lightspeed *
  9.  *  C.  The user is prompted for the command line on program entry.  The *
  10.  *  line is disected and placed in an "argv" array up to a maximum of    *
  11.  *  MAX_ARGS entries.  I/O redirection is also supported, as follows:    *
  12.  *                                                                         *
  13.  *        >         redirects stdout to following file name                     *
  14.  *        <        redirects stdin to following file name                     *
  15.  *        >>        redirects stderr to following file name                     *
  16.  *                                                                         *
  17.  *  File names following a redirection may be immediately after the      *
  18.  *  redirector, or at what appears to be the next argument.  If a file   *
  19.  *  open error occurs, then the program calls "SysBeep" and exits to the *
  20.  *  shell.                                                                 *
  21.  *                                                                         *
  22.  *  TO USE: change the "main" function in you application to "_main" and *
  23.  *  include this file in your project.                                     *
  24.  *                                                                         *
  25.  *************************************************************************/
  26.  
  27. #include    <MacHeaders>
  28. #include    <AppleEvents.h>
  29. #include    <stdarg.h>
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include    "diff.h"
  34.  
  35. int done;
  36.  
  37. #define    MAX_ARGS                     50
  38.  
  39. #ifndef    true
  40. #define    true                          1
  41. #define false                          0
  42. #endif
  43.  
  44. #ifdef    COMMENT
  45. static    int            argc            = 1;        /* final argument count  */
  46. static    char        *argv[MAX_ARGS]    = { "" };    /* array of pointers     */
  47. static    char        command[256];                /* input line buffer     */
  48. static    int            filename         = false;    /* TRUE iff file name     */
  49. #endif
  50.  
  51. int putByte(int len, char *str);
  52. int        resAlloced, resUsed;
  53. Handle    result_handle;
  54.  
  55. /*************************************************************************
  56.  *                                                                         *
  57.  *  Local routine to make a "beep" and exit to the shell.                 *
  58.  *                                                                         *
  59.  *************************************************************************/
  60.  
  61. static void punt()
  62.     {
  63.     SysBeep( 5L );
  64.     exit(1);
  65.     }
  66.  
  67.  
  68. /*************************************************************************
  69.  *                                                                         *
  70.  *  Local routine to open a file in argv[--argc] after closing it's         *
  71.  *  previous existance.                                                     *
  72.  *                                                                         *
  73.  *************************************************************************/
  74.  
  75.  
  76. static void openfile( file, mode, argc, argv, filename )
  77. char                *mode;                        /* mode for file open     */
  78. FILE                *file;                        /* file pointer to use     */
  79. int    *argc;
  80. char    **argv;
  81. int    *filename;
  82.     {
  83.  
  84.     if ( (file = freopen( argv[--*argc], mode, file ) ) <= (FILE *) NULL)
  85.         punt();
  86.     *filename = false;
  87.     }
  88.  
  89.  
  90. /*************************************************************************
  91.  *                                                                         *
  92.  *  New main routine.  Prompts for command line then calls user's main   *
  93.  * now called "_main" with the argument list and redirected I/O.         *
  94.  *                                                                         *
  95.  *************************************************************************/
  96.  
  97. #undef DEBUG
  98.  
  99. #ifdef    DEBUG
  100. char *command = "\"Internal:Desktop Folder:bdir\" \"Internal:Development:Alpha:Tcl:SystemCode\"";
  101. #endif
  102.  
  103. static    void
  104. #ifdef    DEBUG
  105. domain (char *oldcommand, int mainFunc(int argc, char *argv[]))
  106. #else
  107. domain (char *command, int mainFunc(int argc, char *argv[]))
  108. #endif
  109.     {
  110.     char            c;                            /* temp for EOLN check     */
  111.     register char    *cp;                        /* index in command line */
  112.     char            *mode;                        /* local file mode         */
  113.     FILE            *file;                        /* file to change         */
  114.     int i;
  115.     int            argc            = 1;        /* final argument count  */
  116.     char        *argv[MAX_ARGS]    = { "" };    /* array of pointers     */
  117.     int            filename         = false;    /* TRUE iff file name     */
  118.     
  119.     
  120.     cp = &command[0];                            /* start of buffer         */
  121.     argv[0] = "";                                /* program name is NULL  */
  122.     while (argc < MAX_ARGS)
  123.         { /* up to MAX_ARGS entries */
  124.         while (isspace( *cp++ ));
  125.         if ( !*--cp )
  126.             break;
  127.         else if ( *cp == '<' )
  128.             { /* redirect stdin */
  129.             cp++;
  130.             file = stdin;
  131.             mode = "r";
  132.             filename = true;
  133.             }
  134.         else if ( *cp == '>' )
  135.             {
  136.             mode = "w";
  137.             filename = true;
  138.             if (*++cp == '>')
  139.                 {
  140.                 file = stderr;
  141.                 cp++;
  142.                 }
  143.             else
  144.                 file = stdout;
  145.             }
  146.         else
  147.             { /* either an argument or a filename */
  148.             argv[argc++] = cp;
  149.  
  150.             if (*cp == '\"')  {
  151.                 argv[argc - 1]++;
  152.                 while (*++cp && (*cp != '\"'));
  153.             }
  154.             else {
  155.                 while ( *++cp && !isspace( *cp ) );
  156.             }
  157.  
  158.             c = *cp;
  159.             *cp++ = '\0';
  160.             if (filename)
  161.                 openfile( file, mode, &argc, argv, &filename );
  162.             if (!c)
  163.                 break;
  164.             }
  165.         }
  166.     (*mainFunc) ( argc, argv );
  167.     }
  168.  
  169. pascal OSErr AEDoScriptHandler(AppleEvent *message,AppleEvent *reply,long refnum);
  170.  
  171. pascal OSErr
  172. AEDoScriptHandler(AppleEvent *message,AppleEvent *reply,long refnum)
  173. {
  174.     AEDesc             theDesc;
  175.     long            length;
  176.     int        myerr;
  177.     
  178.     result_handle = NewHandle(0);
  179.     if (result_handle != NULL) 
  180.     {
  181.         myerr = AEGetParamDesc(message, keyDirectObject, typeWildCard, & theDesc);
  182.         if (myerr != noErr) return 1;
  183.         
  184.         if (theDesc.descriptorType != (DescType)'TEXT') return 2;
  185.         
  186.         length = GetHandleSize(theDesc.dataHandle);
  187.         SetHandleSize(theDesc.dataHandle, length + 1);
  188.         if (MemError() != noErr)  return 3;
  189.         HLock(theDesc.dataHandle);
  190.         * (*theDesc.dataHandle + length) = '\0';
  191.         
  192.         domain (*theDesc.dataHandle, main_diff2);
  193.  
  194.         HUnlock(theDesc.dataHandle);
  195.  
  196.         HLock(result_handle);
  197.         myerr = AEPutParamPtr( reply, keyDirectObject, typeChar, *result_handle, resUsed );
  198.         HUnlock(result_handle);
  199.         DisposHandle(result_handle);
  200.     }
  201.     done = 1;
  202.     return noErr;
  203. }
  204.  
  205. int putByte(int len, char *str)
  206. {
  207.     
  208. //    return;
  209.     
  210.     if ((resAlloced - resUsed) <= len) {
  211.         if (len > resAlloced) resAlloced += 2 * len;
  212.         else (resAlloced *= 2);
  213.         
  214.         SetHandleSize(result_handle, resAlloced);
  215.         if (MemError() != noErr)  return 4;
  216.     }
  217.     
  218.     HLock(result_handle);
  219.     strncpy(*result_handle + resUsed, str, len);
  220.     resUsed += len;
  221.     HUnlock(result_handle);
  222. }
  223.  
  224.  
  225. int pout(const char *ctrl,...) 
  226. {
  227.     va_list        ap;
  228.     char        str[256];
  229.     
  230.     va_start(ap,ctrl);
  231.     vsprintf(str,ctrl,ap);
  232.     va_end(ap);
  233.     putByte(strlen(str), str);
  234. }
  235.  
  236. static char        outstr[4000];
  237.  
  238. int fpout(FILE *fp, const char *ctrl,...) 
  239. {
  240.     va_list        ap;
  241.     int            len;
  242.     
  243.     va_start(ap,ctrl);
  244.     vsprintf(outstr,ctrl,ap);
  245.     va_end(ap);
  246.     if ((len = strlen(outstr)) > 1000) 
  247.         putByte(10, "<TOO MUCH>");
  248.     else
  249.         putByte(len, outstr);
  250. }
  251.  
  252. int petec(int c, FILE *fp)
  253. {
  254.     char    d = c;
  255.     putByte(1, &d);
  256. }
  257.  
  258. int petc(int c)
  259. {
  260.     char    d = c;
  261.     putByte(1, &d);
  262. }
  263.  
  264. pascal OSErr aeQuit(AppleEvent *, AppleEvent *, long);
  265.  
  266.  
  267. pascal OSErr
  268. aeQuit(AppleEvent *event, AppleEvent *reply, long ref)
  269. {
  270.     exit(1);
  271. }
  272.  
  273. void
  274. main(int argc,char * *argv)
  275. {
  276.     char        command[256];                /* input line buffer     */
  277.     EventRecord    event;
  278.     
  279.     InitGraf(&qd.thePort);
  280.     InitFonts();
  281.     InitCursor();
  282.  
  283. #ifdef    DEBUG
  284.     InitWindows();
  285.     InitMenus();
  286. #endif
  287.  
  288.     TEInit();
  289.     InitDialogs(0L);
  290.     SetApplLimit(((void *) (((long) GetApplLimit()) - 32 * 1024)));
  291.     MaxApplZone();
  292.     MoreMasters();
  293.     MoreMasters();
  294.  
  295.     if (AEInstallEventHandler('misc', 'dosc', AEDoScriptHandler, 0, FALSE) ||
  296.         AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  297.             aeQuit, 0, FALSE)) exit(1);
  298.     
  299. #ifdef    DEBUG
  300.     result_handle = NewHandle(0);
  301.     domain("", main_diff2);
  302.  
  303. #else
  304.  
  305.     while (1)  {
  306.         if (WaitNextEvent(everyEvent,&event, 30, NULL) &&
  307.              (event.what == kHighLevelEvent)) {
  308.             AEProcessAppleEvent(&event);
  309.             if (done) exit(1);
  310.         }
  311.     }
  312. #endif
  313.     exit(1);
  314. }
  315.  
  316.  
  317. size_t pwrite(const void *str, size_t sz, size_t num, FILE *fp)
  318. {
  319.     putByte(sz * num, (char *)str);
  320. }
  321.